package com.nlp.visualization.core.sentence.impl;

import com.hankcs.hanlp.HanLP;
import com.hankcs.hanlp.corpus.dependency.CoNll.CoNLLSentence;
import com.hankcs.hanlp.corpus.dependency.CoNll.CoNLLWord;
import com.nlp.visualization.core.sentence.ISentenceService;
import com.nlp.visualization.core.sentence.SentenceType;
import com.nlp.visualization.pojo.NLP.sen.SentenceCoNLLWord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 句法分析服务，以单条句子作为基本单位，返回以CoNLL格式的词汇
 */
@Service("sentenceServiceImpl")
public class SentenceServiceImpl implements ISentenceService {

    Logger logger = LoggerFactory.getLogger(SentenceServiceImpl.class);

    @Override
    public List<SentenceCoNLLWord> excuteSentenceDependencyAnalyze(String text, SentenceType type) {
        logger.info("执行句法分析：" + text + "，分析方法" + type);
        switch (type) {
            case HANLP_CRF:
                return CRFAnalyze(text);
            case HANLP_NeuralNetwork:
                return NeuralNetWorkAnalyze(text);
            case HANLP_MaxEnt:
                return MaxEntAnalyze(text);
            default:
                return MaxEntAnalyze(text);
        }
    }

    /**
     * 最大底墒算法分析句法
     *
     * @param text
     * @return
     */
    private List<SentenceCoNLLWord> MaxEntAnalyze(String text) {

        CoNLLSentence sentence = HanLP.parseDependency(text);
        return commonHANLP(sentence);

    }

    /**
     * 神经网络分析句法
     *
     * @param text
     * @return
     */
    private List<SentenceCoNLLWord> NeuralNetWorkAnalyze(String text) {
        CoNLLSentence sentence =HanLP.parseDependency(text);
        return commonHANLP(sentence);
    }

    /**
     * CRF算法分析句法
     *
     * @param text
     * @return
     */
    private List<SentenceCoNLLWord> CRFAnalyze(String text) {
        CoNLLSentence sentence = HanLP.parseDependency(text);
        return commonHANLP(sentence);
    }

    /**
     * 复制bean，统一格式化
     *
     * @param sentenceCoNLLWord
     * @param coNLLWord
     */
    private SentenceCoNLLWord CopyCoNLLFormat(SentenceCoNLLWord sentenceCoNLLWord, CoNLLWord coNLLWord) {

        sentenceCoNLLWord.setCPOSTAG(coNLLWord.CPOSTAG);
        sentenceCoNLLWord.setDEPREL(coNLLWord.DEPREL);
        sentenceCoNLLWord.setHEAD(coNLLWord.HEAD.ID+"");
        sentenceCoNLLWord.setID(coNLLWord.ID);
        sentenceCoNLLWord.setLEMMA(coNLLWord.LEMMA);
        sentenceCoNLLWord.setNAME(coNLLWord.NAME);
        sentenceCoNLLWord.setPOSTAG(coNLLWord.POSTAG);
        return sentenceCoNLLWord;

    }

    /**
     * hanlp方式分析句法公共代码部分
     *
     * @param sentence
     * @return
     */
    private List<SentenceCoNLLWord> commonHANLP(CoNLLSentence sentence) {

        List<SentenceCoNLLWord> resList = new ArrayList();
        for (CoNLLWord word : sentence) {
            SentenceCoNLLWord sentenceCoNLLWord = new SentenceCoNLLWord();
            sentenceCoNLLWord = CopyCoNLLFormat(sentenceCoNLLWord, word);
            resList.add(sentenceCoNLLWord);
        }
        return resList;
    }


}
